/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.worker.http;

import org.openislands.oi.worker.app.TaskService;
import org.openislands.oi.constant.RoleType;
import org.openislands.oi.constant.JobState;
import org.openislands.oi.constant.TaskState;
import org.openislands.oi.pojo.dto.OfNodeTaskDTO;
import org.openislands.oi.pojo.dto.CommonResult;
import org.openislands.oi.pojo.dto.JobDTO;
import org.openislands.oi.pojo.dto.TaskDTO;
import org.openislands.oi.pojo.qo.*;
import org.openislands.oi.pojo.vo.OfNodeTaskReportVO;
import org.openislands.oi.pojo.vo.OfNodeTaskVO;
import org.openislands.oi.pojo.vo.CommonResponse;
import org.openislands.oi.pojo.vo.JobVO;
import org.openislands.oi.util.ValueCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("/v1/jobs")
public class TaskController {
    @Resource
    private TaskService taskService;

    @PostMapping("/")
    public CommonResponse<Boolean> createJob(@RequestBody @Validated JobCreateQO createQO) {
        return CommonResponse.parse(taskService.createJob(ValueCopyUtils.copyProperties(createQO, new JobDTO())));
    }

    @DeleteMapping("/{jobId}")
    public CommonResponse<Boolean> deleteJob(@PathVariable(value = "jobId") String jobId) {
        return CommonResponse.parse(taskService.deleteJob(jobId));
    }

    @GetMapping("/{jobId}")
    public CommonResponse<JobVO> queryJob(@PathVariable(value = "jobId") String jobId) {
        CommonResult<JobDTO> result = taskService.queryJob(jobId);
        if (result.isSuccess()) {
            return CommonResponse.parse(result, ValueCopyUtils.copyProperties(result.getData(), new JobVO()));
        } else {
            return CommonResponse.parse(result, null);
        }
    }

    @PutMapping("/{jobId}")
    public CommonResponse<Boolean> updateJob(@PathVariable(value = "jobId") String jobId, @RequestBody @Validated JobUpdateQO updateQO) {
        return CommonResponse.parse(taskService.updateJob(jobId, ValueCopyUtils.copyProperties(updateQO, new JobDTO())));
    }

    @PutMapping("/{jobId}/update/{state}")
    public CommonResponse<Boolean> updateJobState(@PathVariable(value = "jobId") String jobId, @PathVariable(value = "state")JobState toState) {
        return CommonResponse.parse(taskService.updateJobState(jobId, toState));
    }

    @PutMapping("/{jobId}/stop/{state}")
    public CommonResponse<Boolean> stopJob(@PathVariable(value = "jobId") String jobId, @PathVariable(value = "state")JobState toState) {
        return CommonResponse.parse(taskService.stopJob(jobId, toState));
    }

    @PostMapping("/{jobId}/tasks/")
    public CommonResponse<Boolean> createTask(@PathVariable(value = "jobId") String jobId, @RequestBody TaskCreateQO createQO){
        CommonResult<Boolean> result = taskService.createTask(ValueCopyUtils.copyProperties(createQO, new TaskDTO()));
        return CommonResponse.parse(result);
    }

    @PutMapping("/{jobId}/tasks/{taskName}/{taskVersion}")
    public CommonResponse<Boolean> updateTask(@PathVariable(value = "jobId") String jobId, @PathVariable(value = "taskName") String taskName, @PathVariable(value = "taskVersion") Integer taskVersion, @RequestBody TaskUpdateQO updateQO) {
        CommonResult<Boolean> result = taskService.updateTask(jobId, taskName, taskVersion, ValueCopyUtils.copyProperties(updateQO, new TaskDTO()));
        return CommonResponse.parse(result);
    }
    @PutMapping("/{jobId}/tasks/{taskName}/{taskVersion}/update/{state}")
    public CommonResponse<Boolean> updateTaskState(@PathVariable(value = "jobId") String jobId, @PathVariable(value = "taskName") String taskName, @PathVariable(value = "taskVersion") Integer taskVersion, @PathVariable(value = "state")TaskState toState) {
        CommonResult<Boolean> result = taskService.updateTaskState(jobId, taskName, taskVersion, toState);
        return CommonResponse.parse(result);
    }

    @PutMapping("/{jobId}/tasks/{taskName}/{taskVersion}/{role}/start/{state}")
    public CommonResponse<OfNodeTaskVO> startTask(@PathVariable(value = "jobId") String jobId, @PathVariable(value = "taskName") String taskName, @PathVariable(value = "taskVersion") Integer taskVersion, @PathVariable(value = "role") RoleType role, @PathVariable(value = "state")TaskState toState) {
        CommonResult<OfNodeTaskDTO> result = taskService.startTask(jobId, taskName, taskVersion, role, toState);
        return CommonResponse.parse(result, ValueCopyUtils.copyProperties(result.getData(), new OfNodeTaskVO()));
    }

    @PutMapping("/{jobId}/tasks/{taskName}/{taskVersion}/{role}/query")
    public CommonResponse<OfNodeTaskReportVO> queryTaskOfNode(@PathVariable(value = "jobId") String jobId, @PathVariable(value = "taskName") String taskName, @PathVariable(value = "taskVersion") Integer taskVersion, @PathVariable(value = "role") RoleType role) {
        CommonResult<OfNodeTaskDTO> result = taskService.queryTaskOfNode(jobId, taskName, taskVersion, role);
        return CommonResponse.parse(result, ValueCopyUtils.copyProperties(result.getData(), new OfNodeTaskReportVO()));
    }

    @PutMapping("/{jobId}/tasks/{taskName}/{taskVersion}/{role}/activate/{state}")
    public CommonResponse<Boolean> activateTask(@PathVariable(value = "jobId") String jobId, @PathVariable(value = "taskName") String taskName, @PathVariable(value = "taskVersion") Integer taskVersion, @PathVariable(value = "role") RoleType role, @PathVariable(value = "state")TaskState toState) {
        CommonResult<Boolean> result = taskService.activateTask(jobId, taskName, taskVersion, role, toState);
        return CommonResponse.parse(result);
    }

    @PutMapping("/{jobId}/tasks/{taskName}/{taskVersion}/{role}/stop/{state}")
    public CommonResponse<Boolean> stopTask(@PathVariable(value = "jobId") String jobId, @PathVariable(value = "taskName") String taskName, @PathVariable(value = "taskVersion") Integer taskVersion, @PathVariable(value = "role") RoleType role, @PathVariable(value = "state")TaskState toState) {
        CommonResult<Boolean> result = taskService.stopTask(jobId, taskName, taskVersion, role, toState);
        return CommonResponse.parse(result);
    }

    @PutMapping("/{jobId}/tasks/{taskName}/{taskVersion}/{role}/report/{nodeId}")
    public CommonResponse<Boolean> reportTask(@PathVariable(value = "jobId") String jobId, @PathVariable(value = "taskName") String taskName, @PathVariable(value = "taskVersion") Integer taskVersion, @PathVariable(value = "role") RoleType role, @PathVariable(value = "nodeId") String nodeId, @RequestBody OfNodeTaskReportQO ofNodeTaskReportQO) {
        CommonResult<Boolean> result = taskService.reportTask(jobId, taskName, taskVersion, role, nodeId, ValueCopyUtils.copyProperties(ofNodeTaskReportQO, new OfNodeTaskDTO()));
        return CommonResponse.parse(result);
    }
}
